package com.aos.eap.activity;

import android.os.Handler;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.WireInfoBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class MFG_WireDownEqpActivity extends BaseDialogActivity {

    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.rbYes)
    RadioButton rbYes;
    @BindView(R.id.rbNo)
    RadioButton rbNo;
    @BindView(R.id.tvWireId)
    TextView tvWireId;
    @BindView(R.id.tvWireThingId)
    TextView tvWireThingId;
    @BindView(R.id.tvWireLotId)
    TextView tvWireLotId;
    @BindView(R.id.tvThingTime)
    TextView tvThingTime;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvAlreadyDoneNum)
    TextView tvAlreadyDoneNum;
    @BindView(R.id.tvLong)
    TextView tvLong;
    @BindView(R.id.tvLostTime)
    TextView tvLostTime;
    @BindView(R.id.tvDelayTime)
    TextView tvDelayTime;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private WireInfoBean wireInfoBean;
    private int isNull = -1;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_wire_down_eqp;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etEqpId, this, this::requestWireInfo);
    }

    private void requestWireInfo(String s) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", s);
        NetUtils.requestGetNet(this, "EMS_RemoveWireLoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<WireInfoBean>>(
                new TypeToken<BaseResponseBean<WireInfoBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<WireInfoBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    wireInfoBean = response.body().getResponse().getBody();
                    setPageData(wireInfoBean);
                }else {
                    wireInfoBean = null;
                    setPageData(new WireInfoBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<WireInfoBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_RemoveWireLoad");
                wireInfoBean = null;
                setPageData(new WireInfoBean());
            }
        });
    }

    private void setPageData(WireInfoBean wireInfoBean) {
        tvWireId.setText(wireInfoBean.getWireID());
        tvWireThingId.setText(wireInfoBean.getWireMatID());
        tvWireLotId.setText(wireInfoBean.getWireLotID());
        tvThingTime.setText(wireInfoBean.getWireSendTime());
        tvLotId.setText(wireInfoBean.getLotID());
        tvAlreadyDoneNum.setText(String.valueOf(wireInfoBean.getCompleteQty()));
        tvLong.setText(String.valueOf(wireInfoBean.getEqpWireLength()));
        tvLostTime.setText(wireInfoBean.getEqpWireRemainTime());
        tvDelayTime.setText(wireInfoBean.getEqpWireExpireTime());
    }
    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("Wire下机");
        freeUI_titleView.setLeftListener(view -> showBackDialog(MFG_WireDownEqpActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (wireInfoBean == null) {
                showToast("请先扫码获取wire信息", false);
                etEqpId.setText("");
                MyUtils.setEditTextFocus(etEqpId);
                return;
            }
            if (isNull==-1){
                String str="您还未选择卷轴状态，请<font color='#FF0000'><big><b>慎重</b></big></font>选择，选错MH将无法收料！";
                final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
                View contentView = LayoutInflater.from(this).inflate(R.layout.free_ui_golry_dialog_with_confirm_and_cancel, null);
                TextView tvCancel = contentView.findViewById(R.id.tvCancel);
                TextView tvConfirm = contentView.findViewById(R.id.tvConfirm);
                TextView tvTips = contentView.findViewById(R.id.tvTips);
                tvTips.setText(Html.fromHtml(str));
                tvCancel.setVisibility(View.GONE);
                tvConfirm.setText("我知道了");
                tvConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        freeUI_generalFragmentDialog.dismiss();
                    }
                });
                freeUI_generalFragmentDialog.setLayout(contentView);
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "DIALOG");
                return;
            }
            String isNullStr = isNull==1?"空轴":"非空轴";
            String str="您选择了<font color='#FF0000'><big><b>"+isNullStr+"</b></big></font>，请再次确认，选错MH将无法收料！";
            final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
            View contentView = LayoutInflater.from(this).inflate(R.layout.free_ui_golry_dialog_with_confirm_and_cancel, null);
            TextView tvCancel = contentView.findViewById(R.id.tvCancel);
            TextView tvConfirm = contentView.findViewById(R.id.tvConfirm);
            TextView tvTips = contentView.findViewById(R.id.tvTips);
            tvTips.setText(Html.fromHtml(str));
            tvCancel.setText("我再看看");
            tvConfirm.setText("确定提交");
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    freeUI_generalFragmentDialog.dismiss();
                }
            });
            tvConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    freeUI_generalFragmentDialog.dismiss();
                    JsonObject map = new JsonObject();
                    map.addProperty("EqpID", wireInfoBean.getEqpID());
                    map.addProperty("WireID", wireInfoBean.getWireID());
                    map.addProperty("WireBatchNo", wireInfoBean.getWireBatchNo());
                    map.addProperty("WireMatId", wireInfoBean.getWireMatID());
                    map.addProperty("WireLotID", wireInfoBean.getWireLotID());
                    map.addProperty("WireSendTime", wireInfoBean.getWireSendTime());
                    map.addProperty("WireWeight", wireInfoBean.getWireWeight());
                    map.addProperty("WireLength", wireInfoBean.getWireLength());
                    map.addProperty("LotID", wireInfoBean.getLotID());
                    map.addProperty("CompleteQty", wireInfoBean.getCompleteQty());
                    map.addProperty("EqpWireID", wireInfoBean.getEqpWireID());
                    map.addProperty("EqpWireLength", wireInfoBean.getEqpWireLength());
                    map.addProperty("EqpWireRemainTime", wireInfoBean.getEqpWireRemainTime());
                    map.addProperty("EqpWireExpireTime", wireInfoBean.getEqpWireExpireTime());
                    map.addProperty("IsNullReel", isNull==1 ? "Y" : "N");
                    map.addProperty("Remark", etRemark.getText().toString().trim());
                    requestSubmit(map);
                }
            });
            freeUI_generalFragmentDialog.setLayout(contentView);
            freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "DIALOG");
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_RemoveWire", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(MFG_WireDownEqpActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_RemoveWire");
            }
        });
    }

    @OnClick({R.id.rbYes, R.id.rbNo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rbYes:
                isNull = 1;
                String str="请再次确认卷轴实物是否为<font color='#FF0000'><big><b>空轴</b></big></font>，选错MH将无法收料！";
                final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
                View contentView = LayoutInflater.from(this).inflate(R.layout.free_ui_golry_dialog_with_confirm_and_cancel, null);
                TextView tvCancel = contentView.findViewById(R.id.tvCancel);
                TextView tvConfirm = contentView.findViewById(R.id.tvConfirm);
                TextView tvTips = contentView.findViewById(R.id.tvTips);
                tvTips.setText(Html.fromHtml(str));
                tvCancel.setVisibility(View.GONE);
                tvConfirm.setText("我知道了");
                tvConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        freeUI_generalFragmentDialog.dismiss();
                    }
                });
                freeUI_generalFragmentDialog.setLayout(contentView);
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "DIALOG");
                break;
            case R.id.rbNo:
                isNull = 2;
                String str2="请再次确认卷轴实物是否为<font color='#FF0000'><big><b>非空轴</b></big></font>，选错MH将无法收料！";
                final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog2 = new FreeUI_GeneralFragmentDialog();
                View contentView2 = LayoutInflater.from(this).inflate(R.layout.free_ui_golry_dialog_with_confirm_and_cancel, null);
                TextView tvCancel2 = contentView2.findViewById(R.id.tvCancel);
                TextView tvConfirm2 = contentView2.findViewById(R.id.tvConfirm);
                TextView tvTips2 = contentView2.findViewById(R.id.tvTips);
                tvTips2.setText(Html.fromHtml(str2));
                tvCancel2.setVisibility(View.GONE);
                tvConfirm2.setText("我知道了");
                tvConfirm2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        freeUI_generalFragmentDialog2.dismiss();
                    }
                });
                freeUI_generalFragmentDialog2.setLayout(contentView2);
                freeUI_generalFragmentDialog2.show(getSupportFragmentManager(), "DIALOG");
                break;
        }
    }
}
